package com.sofian.android.chat;



import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import org.restlet.data.Method;
import org.restlet.data.Reference;
import org.restlet.resource.ClientResource;
import org.restlet.resource.ResourceException;

import com.sofian.chat.client.ChatMessage;



import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

public class Chat extends Activity {
    private ClientResource cr;
	private Reference reference;
	private ChatResource resource;
	private ClientResource cr_push;
	private ChatResource resource_push;
	private ArrayList<ChatMessage> list;
	private long time;
	private Timer timer;
	private Handler mHandler;
	
	Runnable mUpdateTimeTask;
	private Thread checkUpdate;

	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        initialize();
       
        setContentView(R.layout.chat);
       
    }
    
    void update()
    {
    	
    	reference = new Reference("http://sofian-gwt.appspot.com/chat/hello");
    	 reference.addQueryParameter("time", String.valueOf(time));
         cr.getRequest().setResourceRef(reference);
         cr.getRequest().setMethod(Method.GET);
    	 resource = cr.wrap(ChatResource.class);
        ChatMessage[] tlist = resource.getNewMessages();
        
        if(tlist.length>0)
        {
          
          for(ChatMessage m : tlist)
          {
        	  list.add(m);
          }
          
          
          time = System.currentTimeMillis();
          mHandler.sendEmptyMessage(0);
        }
        
        
    }
    
    void print()
    {
    	((LinearLayout)this.findViewById(R.id.linearLayout2)).removeAllViews();
        for(ChatMessage m : list)
        {
        	
        	TextView row = new TextView(this);
        	row.append(m.getSender()+"\n");
        	row.append((new Date(m.getTimestamp())).toGMTString()+"\n");
        	row.append(m.getMessage()+"\n");
        	
        	     	
        	
        	((LinearLayout)this.findViewById(R.id.linearLayout2)).addView(row);
        	
        }
    }
    
    void initialize()
    {
    	mHandler = new Handler();
    	
    	mUpdateTimeTask = new Runnable() {
 		   public void run() {
 		    print();
 		    
 		   }
 		};
    	
 		
 		
 		
    	
    	 // Initialize the resource proxy.
        cr = new ClientResource("http://sofian-gwt.appspot.com/chat/hello");
       time = System.currentTimeMillis();
      
         reference = new Reference("http://sofian-gwt.appspot.com/chat/hello"); 
        reference.addQueryParameter("time", String.valueOf(time));
        cr.getRequest().setResourceRef(reference);
        cr.getRequest().setMethod(Method.GET);
      
       
       cr_push = new ClientResource("http://sofian-gwt.appspot.com/chat/hello");
 
       
       resource_push = cr_push.wrap(ChatResource.class);
       
       checkUpdate = new Thread() {
   	    public void run() {
   	       update();
   	       mHandler.post(mUpdateTimeTask);
   	       mHandler.postDelayed(checkUpdate, 2000);
   	    }
   	};
       
       mHandler.removeCallbacks(checkUpdate);
       mHandler.postDelayed(checkUpdate, 2000);
       
       
       
       list = new ArrayList<ChatMessage>();
       
       
     
    }
    
    public void Send(View v)
    {
    	new Thread() {
       	    public void run() {
       	    	put();
       	    }
       	    }.run();
    	
    	
    }
    
    
    public void put()
    {
    	String message = ((EditText)findViewById(R.id.editText1)).getText().toString();
    	String name = ((EditText)findViewById(R.id.editText2)).getText().toString();
    	resource_push.sendMessage(name, message);
    }
    
    
    
    
}

